/**
 * @Title: involve.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author ex-xieqi001
 * @date 2014年11月17日 下午11:48:10
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.directives');
	app.directive('selectContainer', ['$parse','BaseURL',function($parse,baseUrl){
		
		function ctrler($scope, $element, $attrs, $timeout) {
			
			//存储所有值
			var datas = [];
			
			//返回的值  false：对象， true ：code属性值
			$scope.type = $scope.type ||  false;
			
			//判断是否需要删除按钮
			$scope.allowClear =  false;
			
			//是否显示提示文字
			$scope.results = false;
			
			//下拉集合
			$scope.containerDatas = [];
			
			//查询的对象属性值集合
			var searchTypes = [];
			//监听数组变化
			$scope.$watchCollection('containerData',function(v){
				if (v) {
					$scope.results = false;
					datas = [];
					$scope.containerDatas = v;
					selectResult();
					angular.forEach(v,function(data,index){
						datas.push(data);
					});
				}
				else {
					$scope.results = true;
					$scope.results_error = '正在加载中...';
					$scope.containerDatas = [];
					datas = [];
				}
			});
			
			//监听需要查询的对象属性值集合
			$scope.$watchCollection('searchDatas',function(v){
				if (v) {
					searchTypes = v;
				} 
				else {
					searchTypes = [$scope.text];
				}
			});
			
			//监听选中值变化
			$scope.$watchCollection('result',function(v){
				if (v) {
					$scope.allowClear = $scope.clear;
					selectResult();
				}
				else {
					$scope.selectData = null;
					$scope.allowClear = false;
				}
			});
			
			function selectResult() {
				if ($scope.type) {
					angular.forEach($scope.containerDatas,function(data,index){
						if ($scope.result == data[$scope.code]) {
							$scope.selectData = data;
						}
					});
				}
				else {
					$scope.selectData = $scope.result;
				}
			}
			
			
			//是否显示
			$scope.containerShow = false;
			
			//是否还在搜索框内
			var isShow = true;
			
			//是否展示搜索框事件  点击事件
			$scope.selectContainerShow = function() {
				isShow = false;
				$scope.containerShow = !$scope.containerShow;
				if ($scope.containerShow) {
					$scope.searchName = '';
					$scope.containerDatas = datas;
					//获取文本框的宽度
					$scope.width = $element.children().find(".select2-container").context.offsetWidth;
				}
			}
			
			//是否展示搜索框事件  获取焦点事件
			$scope.focusShow = function() {
				isShow = false;
				$scope.containerShow = true;
			}
			
			//隐藏下拉框 失去焦点事件
			$scope.cancel = function() {
				isShow = true;
				$timeout(function(){
					if (isShow) {
						$scope.containerShow = false;
					}
				}, 200);
			}
			
			//删除选中的值
			$scope.closeSelect = function () {
				$scope.allowClear = false;
				$scope.result = null;
				$scope.containerShow = true;
			}
			
			//点击选中事件
			$scope.clickSelectData = function(data) {
				$scope.selectData = data;
				
				if ($scope.type) {
					$scope.result = $scope.selectData[$scope.code];
				}
				else {
					$scope.result = $scope.selectData;
				}
				$scope.containerShow = false;
			}
			
			//查询
			$scope.search = function() {
				if ($scope.searchName) {
					var searchContainerDatas = [];
					angular.forEach(datas,function(data,index){
						var isFlag = true;
						angular.forEach(searchTypes,function(node,index){
							data[node] = data[node] ? data[node].toLowerCase():'';
							if(isFlag && data[node].indexOf($.trim($scope.searchName).toLowerCase()) > -1){
								searchContainerDatas.push(data);
								isFlag = false;
							}
						});
					});
					if (!searchContainerDatas || searchContainerDatas == '') {
						$scope.results = true;
						$scope.results_error = '找不到匹配项';
					}
					else {
						$scope.results = false;
					}
					$scope.containerDatas = searchContainerDatas;
				}
				else {
					$scope.containerDatas = datas;
				}
			}
		}
		return {
			restrict: 'EA',
			scope:{
				code: '@code',
				text: '@text',
				containerData : '=containerData',
				searchDatas : '=searchDatas',
				type : '=type',
				clear : '=clear',
				result : '=result'
			},
			replace:true,
			templateUrl:baseUrl + 'views/hcpms/common/directives/selectContainer.html',
			controller:ctrler
		};
		
	}]);
	
})();